package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.scd.bean.Order;
import com.kgc.scd.bean.OrderVO;
import com.kgc.scd.mapper.OrderMapper;
import com.kgc.scd.service.OrderService;
import com.kgc.scd.util.PageSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangguoqiang
 * @since 2022-11-14
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired(required = false)
    private OrderMapper orderMapper;
    @Autowired(required = false)
    private OrderService orderService;

    /**
     * @param
     * @param pageNo
     * @param pageSize
     * @return : com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.kgc.scd.bean.Comment>
     * @author : Administrator
     * @date : 2022/11/15
     * @description : 查询订单带分页
     */
    @Override
    public PageSupport<Order> getOrderListAndPage(Integer shoppingStatus, Integer pageNo, Integer pageSize) {
        // 创建统一返回分页对象
        PageSupport<Order> pageSupport = new PageSupport<>();

        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
        if (!Objects.isNull(shoppingStatus)){
            wrapper.eq(Order::getShoppingStatus,shoppingStatus);
        }

        //分页
        Page<Order> evaPage = new Page<>(pageNo,pageSize);
        Page<Order> evaIPage = orderMapper.selectPage(evaPage, wrapper);


        // 添加分页的页码数据
        pageSupport.setPageNo(pageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalPage((int) evaIPage.getPages());
        pageSupport.setTotalCount((int) evaIPage.getTotal());
        pageSupport.setPageData(evaIPage.getRecords());
        log.info("--- 总条数：{} ---",evaIPage.getTotal());
        log.info("--- 总页数：{} ---",evaIPage.getPages());



        return pageSupport;
    }


    /**
     * @author : Administrator
     * @date   : 2022/11/16
     * @param  : [id]
     * @return : boolean
     * @description : 根据id，删除已收货的订单
     */
    @Override
    public boolean delOrderList(String id) {
        //根据id获取订单信息，再去比较订单状态是否等于8（已追评）
        Order order = getById(id);
        if (order.getShoppingStatus().equals(8)){
            return orderMapper.deleteById(id) > 0 ;
        }
        return false;
    }

    /**
     * @param orderNo
     * @return : com.kgc.scd.bean.OrderVO
     * @author : Administrator
     * @date : 2022/11/16
     * @description : 根据订单号查询订单
     */
    @Override
    public OrderVO queryOrderByNo(String orderNo) {

        return orderMapper.selectOrderByNo(orderNo);
    }

    /**
     * @param orderNo
     * @param expressNo
     * @return : int
     * @author : Administrator
     * @date : 2022/11/16
     * @description : 插入订单快递单号
     */
    @Override
    public int insertExpressNo(String orderNo, String expressNo) {
        /*LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getOrderNo,orderNo);*/
        Order orders = new Order();
        orders.setId(orderNo);
        orders.setLogisticCode(expressNo);
        orders.setShoppingStatus(5);
        return orderMapper.updateById(orders);
    }


    /**
     * @author : Administrator
     * @date   : 2022/11/18
     * @param  : [id, state]
     * @return : int
     * @description : 增加评论和追评的统一修改状态
     */
    @Override
    public int updateOrderState(String id, Integer state) {
//        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Order::getOrderNo,orderNo);
        Order orders = new Order();
        orders.setId(id);
        orders.setShoppingStatus(state);
        return orderMapper.updateById(orders);
    }

    /**
     * @param id
     * @return : int
     * @author : Administrator
     * @date : 2022/11/18
     * @description : 根据id查询数据库中的状态，修改订单状态为7，6是已签收
     */
    @Override
    public boolean selectOrderState(String id) {

        if (orderMapper.selectById(id).getShoppingStatus() == 6){
            return true;
        }
        return false;
    }

    /**
     * @param id
     * @return : int
     * @author : Administrator
     * @date : 2022/11/18
     * @description : 根据订单id查询数据库中的状态，修改订单状态为8，7是已评价
     */
    @Override
    public boolean selectOrdersState(String id) {
        if (orderMapper.selectById(id).getShoppingStatus() == 7){
            return true;
        }
        return false;
    }

    /**
     * @param id
     * @return : boolean
     * @author : Administrator
     * @date : 2022/11/18
     * @description : 根据订单id来签收订单
     */
    @Override
    public boolean updateStatusSign(String id) {
        Order order = orderMapper.selectById(id);
        if (order.getShoppingStatus() == 5){
            this.orderService.updateOrderState(order.getId(),6);
            return true;
        }
        return false;
    }
}
